### wulanbucha
// If there is not specifying vpc_id, the module will launch a new vpc
resource "alicloud_vpc" "vpc_wlbc" {
  provider   = alicloud.wulanchabu
  cidr_block = local.vpc_cidr
  vpc_name   = local.vpc_name
}


// According to the vswitch cidr blocks to launch several vswitches
resource "alicloud_vswitch" "vswitches_wlbc" {
  provider          = alicloud.wulanchabu
  count             = length(local.vswitch_cidrs)
  vpc_id            = alicloud_vpc.vpc_wlbc.id
  cidr_block        = local.vswitch_cidrs[count.index]
  zone_id           = element(local.wlch_zones, count.index) 
  vswitch_name      = format("%s-%s",local.vswitch_name_prefix, count.index + 1)
}

resource "alicloud_nat_gateway" "k8s_gateway_wlbc" {
  provider              = alicloud.wulanchabu
  count                 = length(local.vswitch_cidrs)
  vpc_id                = alicloud_vpc.vpc_wlbc.id
  nat_gateway_name      = format("%s-%s",local.nat_gateway_name, count.index + 1)
  nat_type              = "Enhanced"
  deletion_protection   = true
  vswitch_id            = element(alicloud_vswitch.vswitches_wlbc.*.id , count.index)
}

resource "alicloud_eip" "k8s_eip_wlbc" {
  provider              = alicloud.wulanchabu
  count                 = length(local.vswitch_cidrs) 
  bandwidth             = 10
  deletion_protection   = true
}

resource "alicloud_eip_association" "k8s_eip_ass_wlbc" {
  provider      = alicloud.wulanchabu
  count         = length(local.vswitch_cidrs) 
  allocation_id = element(alicloud_eip.k8s_eip_wlbc.*.id, count.index)
  instance_id   = element(alicloud_nat_gateway.k8s_gateway_wlbc.*.id, count.index) 
}

resource "alicloud_snat_entry" "k8s_snat_entry_wlbc" {
  provider          = alicloud.wulanchabu
  count             = length(local.vswitch_cidrs) 
  snat_table_id     = element(alicloud_nat_gateway.k8s_gateway_wlbc.*.snat_table_ids, count.index)
  source_vswitch_id = element(alicloud_vswitch.vswitches_wlbc.*.id, count.index)  #split(",", join(",", alicloud_vswitch.vswitches.*.id))[count.index % length(split(",", join(",", alicloud_vswitch.vswitches.*.id)))]
  snat_ip           = element(alicloud_eip.k8s_eip_wlbc.*.ip_address, count.index)
  depends_on        = [alicloud_eip_association.k8s_eip_ass_wlbc]
}